package com.jc.gameconter.controller.order;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jc.business.userorder.UserOrderBusiness;
import com.jc.cds.model.CustomUserOrder;
import com.jc.common.util.CommonStr;
import com.jc.ds.base.service.BaseServiceException;
import com.jc.ds.model.UserOrder;
import com.jc.framework.util.PropertyPlaceholder;
import com.jc.gameconter.controller.BaseController;
import com.jc.service.appgame.AppGameService;
import com.jc.service.chain.ChainResult;
import com.jc.service.userorder.UserOrderService;

@Controller
@RequestMapping("order")
public class OrderQueryController extends BaseController {

	// 公司账号用户ID
	final String companyUserId = (String) PropertyPlaceholder.getProperty("company.user.id");

	final String currencyName = (String) PropertyPlaceholder.getProperty("transfer.currency.name");

	@Autowired
	private AppGameService appGameService;

	@Autowired
	UserOrderBusiness userOrderBusiness;

	@Autowired
	private UserOrderService userOrderService;

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

	}

	@RequestMapping(value = "/orderlist", method = RequestMethod.GET)
	public @ResponseBody String orderlist(HttpServletRequest request, UserOrder userOrder, Integer pageNum,
			Integer pageSize) {

		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 15;
		PageInfo<CustomUserOrder> page = userOrderBusiness.queryAllOrderHistory(userOrder, pageNum, pageSize);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("total", page.getTotal());
		jsonObject.put("rows", page.getList());
		return jsonObject.toJSONString();

	}

	@RequestMapping(value = "/sumMoney", method = RequestMethod.GET)
	public @ResponseBody String querySumMoney(HttpServletRequest request) {
		// 查询APP游戏总人数和总金币数
		JSONObject ret = appGameService.queryTotal();
		// 查询链上ＶＲＴ总数（公司账户）
		ChainResult result = userOrderService.queryBalance(Long.valueOf(companyUserId), currencyName);
		// 验证返回结果
		if (result == null || !StringUtils.equals(result.getReturn_code(), CommonStr.SUCCESS)) {
			throw new BaseServiceException("VRT链上查询错误! " + (result == null ? "" : result.getReturn_msg()));
		}
		ret.put("vrtMoney", result.getBalance());

		return ret.toString();
	}

}
